package framework
{
    import flash.events.Event;
    import flash.utils.setTimeout;

    public class CommandEvent extends Event
    {
        public static const COMMAND:String = "command";

        public var beanId:String;

        public var method:String;

        public var args:Array;

        public var contextName:String;

        protected var alreadyDispatched:Boolean;

        public function CommandEvent(beanId:String, method:String = null, args:Array = null, contextName:String = "default", autoDispatch:Boolean = true, babbles:Boolean = false, cancelable:Boolean = false)
        {
            this.beanId = beanId;
            this.method = method;
            this.args = args;
            this.contextName = contextName;
            super(COMMAND, babbles, cancelable);
            if (autoDispatch)
            {
                setTimeout(internalDispatch, 1);
            }
        }

        private function internalDispatch():void
        {
            if (!alreadyDispatched)
                dispatch();
        }

        public function dispatch():void
        {
            Ctx.contexts[contextName].dispatchEvent(this);
            alreadyDispatched = true;
        }

    }
}